var t = getApp(), a = require("../../../mybehaviors/location/location");

Page({
    behaviors: [ a ],
    data: {
        storeGoodsList: [],
        searchStoreList: [],
        storeName: "",
        longitude: "",
        latitude: "",
        goodsPage: 1,
        goodsLastPage: 0,
        goodsMore: !1,
        storePage: 1,
        storeLastPage: 0,
        storeMore: !1
    },
    onLoad: function(t) {
        var a, o = this;
        if ("" !== (null !== (a = t.keyword) && void 0 !== a ? a : "")) {
            this.setData({
                storeName: t.keyword
            }), t.keyword.trim();
            var e, s = wx.getStorageSync("searchHistory") || [];
            if (s.length > 0) {
                var r = (s = JSON.parse(s)).length;
                console.log(r), r >= 10 && s.pop();
            }
            var n = Date.parse(new Date());
            s.unshift({
                name: t.keyword,
                key: n
            }), e = s.filter(function(t, a, o) {
                return o.findIndex(function(a) {
                    return a.name === t.name;
                }) === a;
            }), wx.setStorageSync("searchHistory", JSON.stringify(e)), this.searchGoods();
        }
        try {
            wx.removeStorageSync("applyaddresspoint");
        } catch (t) {}
        this._getLocation(function(t, a) {
            console.log(a), o.setData({
                longitude: a.lng,
                latitude: a.lat
            }), o.searchStore(), console.log(o.data.longitude);
        });
    },
    onReady: function() {},
    onShow: function() {
        try {
            var t = wx.getStorageSync("applyaddresspoint");
            console.log(t), t && (console.log(t), this.setData({
                longitude: e.lng,
                latitude: e.lat
            }), console.log(this.data.longitude));
        } catch (t) {
            console.log(t);
        }
    },
    storeInput: function(t) {
        console.log(t), this.setData({
            storeName: t.detail.value
        });
    },
    commercialOn: function(t) {
        var a = t.currentTarget.dataset.id;
        wx.navigateTo({
            url: "/packageF/consumption/commodity/commodity?storeId=" + a
        });
    },
    goodsNavTo: function(t) {
        var a = t.currentTarget.dataset.goodsid;
        wx.navigateTo({
            url: "/packageA/detail_v2/detail_v2?id=" + a
        });
    },
    searchClick: function() {
        var t;
        "" !== (null !== (t = this.data.storeName) && void 0 !== t ? t : "") && (this.searchStore(), 
        this.searchGoods());
    },
    searchStore: function() {
        var a = this, o = t.getNetAddresss("plugin.coupon-store.frontend.search.searchStore"), e = {
            store_name: this.data.storeName,
            latitude: this.data.latitude,
            longitude: this.data.longitude,
            page: this.data.storePage
        };
        t._postNetWork({
            url: o,
            data: e,
            success: function(t) {
                var o = t.data;
                1 == o.result ? (console.log(o), a.data.storeLastPage || (a.data.storeLastPage = 0), 
                a.setData({
                    searchStoreList: o.data.data,
                    storeMore: !0
                })) : (a.data.storePage = a.data.storePage - 1, a.goodsMore = !1, wx.showToast({
                    icon: "none",
                    title: o.msg,
                    duration: 1500
                }));
            },
            fail: function(t) {
                console.log(t);
            }
        });
    },
    searchGoods: function() {
        var a = this, o = t.getNetAddresss("plugin.coupon-store.frontend.search.searchGoods"), e = {
            search: {
                keyword: this.data.storeName
            },
            page: this.data.goodsPage
        };
        t._postNetWork({
            url: o,
            data: e,
            success: function(t) {
                var o = t.data;
                1 == o.result ? (console.log(o), a.data.goodsLastPage || (a.data.goodsLastPage = 0), 
                a.setData({
                    storeGoodsList: o.data.data,
                    goodsMore: !0,
                    goodsLastPage: o.data.last_page
                })) : (a.goodsMore = !1, wx.showToast({
                    icon: "none",
                    title: o.msg,
                    duration: 1500
                }));
            },
            fail: function(t) {
                console.log(t);
            }
        });
    },
    goodsMoreData: function() {
        var a = this;
        if (this.data.goodsMore = !1, this.data.goodsPage >= this.data.goodsLastPage) console.log("2343242"); else {
            this.data.goodsPage = this.data.goodsPage + 1;
            var o = t.getNetAddresss("plugin.coupon-store.frontend.search.searchGoods"), e = {
                search: {
                    keyword: this.data.storeName
                },
                page: this.data.goodsPage,
                paginate: 5
            };
            t._getNetWork({
                url: o,
                data: e,
                success: function(t) {
                    var o = t.data, e = o.data.data;
                    a.data.goodsMore = !0, 1 == o.result ? a.setData({
                        storeGoodsList: a.data.storeGoodsList.concat(e),
                        goodsLastPage: o.data.last_page
                    }) : wx.showToast({
                        icon: "none",
                        title: o.msg,
                        duration: 1500
                    }), console.log(a.data.storeGoodsList);
                },
                fail: function(t) {
                    console.log(t);
                }
            });
        }
    },
    storeMoreData: function() {
        var a = this;
        if (this.data.goodsMore = !1, !(this.data.storePage >= this.data.storeLastPage)) {
            this.data.storePage = this.data.storePage + 1;
            var o = t.getNetAddresss("plugin.coupon-store.frontend.search.searchStore"), e = {
                search: {
                    keyword: this.data.storeName
                },
                page: this.data.storePage
            };
            t._getNetWork({
                url: o,
                data: e,
                success: function(t) {
                    var o = t.data, e = o.data.data;
                    1 == o.result ? (a.data.storeMore = !0, a.setData({
                        searchStoreList: a.data.searchStoreList.concat(e),
                        storeLastPage: o.data.last_page
                    })) : (a.data.storePage = a.data.storePage - 1, a.storeMore = !1, wx.showToast({
                        icon: "none",
                        title: o.msg,
                        duration: 1500
                    }));
                },
                fail: function(t) {
                    console.log(t);
                }
            });
        }
    },
    onHide: function() {},
    onUnload: function() {},
    onPullDownRefresh: function() {},
    onReachBottom: function() {},
    onShareAppMessage: function() {}
});